import { credit } from "../../../../js/modules/credit.js";
import { orderPay } from "../../../../js/modules/pay.js";
import { api,forms, storage, util, dialog } from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
const app = getApp();
BasePage({
  pageKey:"billDetail",

  /**
   * 页面的初始数据
   */
  data: {
    modalType:1,
    bill_detail:{},
    prePay_detail:{},
    repay_order_amount: 0.00,
    arr_order: [],
    headerBg:"",
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断是否登录
    if (!authorize.isLogin('store')) {
      authorize.redirect('store');
      return;
    }

    if(options.id==null){
      util.alert("请求无效");
      return;
    }
    var that=this;
    that.setData({
      billId:options.id,
      accountId:options.accountId
    });

  },
  /**
     * 跳转到收营台
     */
  onSkipPayEvent: function (e) {
    router.navigate({
      path: "billPayComfirm",
      params: {
        billId: this.data.billId,
      }
    });
  },
  /**
   * 金额输入事件
   */
  onBindInput: function (e) {

    this.data.prePay_detail.ShouldAmount = e.detail.value;
    this.setData({
      prePay_detail: this.data.prePay_detail
    });
  },
  /**
   * 按账单还款
   */
  onRepayWithBillEvent:function(){
    var amount = this.data.prePay_detail.ShouldAmount;
    if (!forms.validate({
      value: amount,
      validate: "num",
      error: "请输入正确的金额"
    })) {
      return;
    };
    amount = parseFloat(amount);
    var shouldRepayAmount = parseFloat(this.data.prePay_detail.ShouldRepayAmount);
    if (shouldRepayAmount < amount){
      util.tips("还款金额大于实际还款金额");
      return;
    }
    credit.pay_bill(0, this.data.accountId, this.data.billId, 0, amount,function(content){

    });
  },
  onCancelRepayEvent(){
    var that=this;
    credit.cancelRepay(this.data.prePay_detail.Id,function(){
      that.hideModal();
      that.getBillDetail(that);
    });
  },
  /**
   * 显示订单信息
   */
  onShowOrdersEvent:function(e){
    var that = this;
    if (this.data.bill_detail.ShouldAmount<=0){
      util.alert("还款金额必须大于0！");
      return;
    }
    credit.get_bill_orders(this.data.billId, function (content) {
      that.setData({
        order_list: content
      });
      that.showModal(e);
    });

  },
  /**
 * 计算
 */
  caculate: function () {
    var that = this;
    var repay_order_amount = 0, arr_order = [];
    for (var i = 0; i < that.data.order_list.length; i++) {
      if (that.data.order_list[i].Checked) {
        var item = that.data.order_list[i];
        repay_order_amount += item.PayAmount;
        arr_order.push(item.Id);
      }
    }
    that.setData({
      order_list: that.data.order_list,
      repay_order_amount: repay_order_amount,
      arr_order: arr_order
    });
  },
  /**
  * 单个选择事件
  */
  onSelectSingleEvent: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.order_list[index].Checked = !(that.data.order_list[index].Checked || false);
    that.caculate();
  },
  /**
   * 全选事件
   */
  onSelectAllEvent: function (e) {
    var that = this;
    that.data.select_all_status = (that.data.select_all_status == 1 ? 0 : 1);
    for (var i = 0; i < that.data.order_list.length; i++) {
      that.data.order_list[i].Checked = that.data.select_all_status;
    }
    that.caculate();
  },
  /**
   * 按订单还款
   */
  onRepayWithOrderEvent:function(){
    credit.pay_bill(1, this.data.accountId, this.data.billId, this.data.arr_order.join(","), this.data.repay_order_amount, function (content) {
      orderPay.findPayOrderData(content.PaySign, "cashier");
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥'
    })
    this.getBillDetail();
  },
  getBillDetail(that){
    that = that||this;
    //加载账单详情
    credit.get_bill_detail(that.data.billId, function (res) {
      res = res.data;
      if (res.Success) {
        var data = res.Content;
        data.ShouldAmount = (data.RepayAmount - data.AccountPaid - data.RefundAmount).toFixed(2);
        data.ShouldRepayAmount = parseFloat(data.ShouldAmount) + data.ShouldOverdueAmount;
        data.IsOverdue = (new Date(data.LastRepayTime) < new Date());
        that.setData({
          bill_detail: data,
          prePay_detail: data
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 展开遮罩层
   */
  showModal: function (e) {
    var _type = e.currentTarget.dataset.type;
    this.setData({
      modalType:_type
    });
    dialog.showModal(this, 'showModalStatus')
  },
  /**
   * 隐藏遮罩层
   */
  hideModal: function () {
    dialog.hideModal(this, 'showModalStatus')
  }
})